﻿using ET.DBCommon.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.DBCommon.DAL
{
    public class SubmitelementDAL
    {
        public static List<Submitelement> GetSubmitelementListByTestQuestionID(int TestQuestionID)
        {
            string sql = "SELECT * FROM Submitelement where TestQuestionID = " + TestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<Submitelement> list = new List<Submitelement>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetSubmitelementModel(dt.Rows[i]));
                }
            }
            return list;
        }

        public static List<Submitelement> GetSubmitelementListByExamQuestionID(int ExamQuestionID)
        {
            string sql = "SELECT a.* FROM Submitelement a inner join examresultstestquestion b on a.TestQuestionID=b.TestQuestionID where b.Id = " + ExamQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<Submitelement> list = new List<Submitelement>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetSubmitelementModel(dt.Rows[i]));
                }
            }
            return list;
        }

        public static bool InsertSubmitelement(out int newId, Submitelement submitelement)
        {
            string sql = "insert into Submitelement (ID,DisplayText,Answer,IsTime,TestQuestionID) values (@ID,@DisplayText,@Answer,@IsTime,@TestQuestionID)";
            MySqlParameter[] parms = {
                new MySqlParameter("@ID", submitelement.ID),
                new MySqlParameter("@DisplayText", submitelement.DisplayText),
                new MySqlParameter("@Answer", submitelement.Answer),
                new MySqlParameter("@IsTime", submitelement.IsTime),
                new MySqlParameter("@TestQuestionID", submitelement.TestQuestionID)
            };

            return DbHelper.ExecuteNonQuery(out newId, sql, parms) > 0;
        }

        public static bool UpdateSubmitelement(Submitelement submitelement)
        {
            string sql = "update Submitelement set `DisplayText` = @DisplayText,Answer=@Answer,IsTime=@IsTime WHERE ID = @ID";
            MySqlParameter[] parms = {
                new MySqlParameter("@ID", submitelement.ID),
                new MySqlParameter("@DisplayText", submitelement.DisplayText),
                new MySqlParameter("@Answer", submitelement.Answer),
                new MySqlParameter("@IsTime", submitelement.IsTime)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        public static bool DeleteSubmitelement(int ID)
        {
            string sql = "delete from Submitelement WHERE ID = @ID";
            MySqlParameter[] parms = {
                new MySqlParameter("@ID", ID)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        internal static Submitelement GetSubmitelementModel(DataRow dr)
        {
            Submitelement info = new Submitelement();

            info.ID = Convert.ToInt32(dr["ID"].ToString());
            info.TestQuestionID = Convert.ToInt32(dr["TestQuestionID"]);
            info.Answer = dr["Answer"].ToString();
            info.DisplayText = dr["DisplayText"].ToString();
            info.IsTime = bool.Parse(dr["IsTime"].ToString());
            return info;
        }
    }
}
